The typeof
operator returns a string indicating the type of the unevaluated operand. It is useful
for determining the type of a variable or expression in JavaScript.
The syntax for the typeof
operator is:
typeof operand
Here, operand
is the expression whose type is to be returned.
The typeof
operator can return the following values:
Here are some examples of using the typeof
operator:
typeof "Hello"; // Returns "string"
typeof 42; // Returns "number"
typeof true; // Returns "boolean"
typeof undefined; // Returns "undefined"
typeof {name: "John"}; // Returns "object"
typeof [1, 2, 3]; // Returns "object"
typeof function() {}; // Returns "function"
typeof null; // Returns "object" (this is a known quirk in JavaScript)